﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using ApplicationGenerator.Versions.BusinessLogic;
using ApplicationGenerator.Versions.Forms;
using System.Drawing.Design;
using System.Collections.Generic;

namespace WorkflowLibrary
{
    /// <summary>
    /// Aktywność informująca o końcu procesu.
    /// </summary>
    public partial class StopProcessActivity : SequenceActivity, IActivityName//, ICustomTypeDescriptor
    {

        #region Events and event handlers

        public delegate void StopProcessEventHandler(object sender,
            StopProcessEventArgs e);
        private event StopProcessEventHandler processStoppedEvent;
        public event StopProcessEventHandler ProcessStopped
        {
            add
            {
                processStoppedEvent += value;
            }
            remove
            {
                processStoppedEvent -= value;
            }
        }

        #endregion

        #region Construction

        public StopProcessActivity()
        {
            InitializeComponent();
            StopProcessVersionInfo = new VersionInfo();
            StopProcessVersionInfo.Type = VersionElement.ProcessStopped;
        }

        #endregion

        #region Dependency properties

        /// <summary>
        /// contains informations about form to be shown
        /// </summary>
        public static DependencyProperty StopProcessVersionInfoProperty =
                    DependencyProperty.Register("StopProcessVersionInfo",
                    typeof(VersionInfo),
                    typeof(StopProcessActivity));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(false)]
        [DescriptionAttribute("Informacje o wesji")]
        [CategoryAttribute("ShowForm Property")]
        [DisplayName("")]
        [TypeConverter(typeof(VersionConverter))]
        [Editor(typeof(VersionEditor), typeof(UITypeEditor))]
        [VersionType(VersionElement.ProcessStopped)]
        public VersionInfo StopProcessVersionInfo
        {
            get
            {
                return ((VersionInfo)(base.GetValue(StopProcessActivity.StopProcessVersionInfoProperty)));
            }
            set
            {
                base.SetValue(StopProcessActivity.StopProcessVersionInfoProperty, value);
            }
        }

        #endregion

        #region Code methods

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            if (processStoppedEvent != null)
            {
                processStoppedEvent(null, new StopProcessEventArgs(StopProcessVersionInfo));
            }
        }

        #endregion

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection colAvailableCollection = TypeDescriptor.GetProperties(this, attributes, true);
            List<PropertyDescriptor> colDisplayProperties = new List<PropertyDescriptor>();
            colDisplayProperties.Add(colAvailableCollection["ActivityName"]);

            return new PropertyDescriptorCollection(colDisplayProperties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion

        #region IActivityName Members
        [Category("Dane podstawowe")]
        [DisplayName("Nazwa czynności")]
        [Description("Nazwa czynności przyjazna dla użytkownika")]
        public string ActivityName
        {
            get;
            set;
        }

        #endregion


    }

    #region Event Args class

    public class StopProcessEventArgs
    {

        private VersionInfo versionInfo;


        public StopProcessEventArgs(VersionInfo _formVersionInfo)
        {
            this.versionInfo = _formVersionInfo;
            this.versionInfo.Type = VersionElement.ProcessStopped;
        }

        public VersionInfo VersionInfo
        {
            get { return versionInfo; }
        }
    }

    #endregion
}
